'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Namespace Exceptions
    Public Class ExceptionService
        Inherits Service
        Implements Interfaces.Exceptions.IExceptionService

        Private _Exceptions As NotifyingCollection(Of Interfaces.Exceptions.IExceptionInformation)
        Private _ReadOnlyExceptions As ReadOnlyNotifyingCollection(Of Interfaces.Exceptions.IExceptionInformation)

        Public Sub New()
            _Exceptions = New NotifyingCollection(Of Interfaces.Exceptions.IExceptionInformation)
            _ReadOnlyExceptions = New ReadOnlyNotifyingCollection(Of Interfaces.Exceptions.IExceptionInformation)(_Exceptions)
        End Sub

        Public ReadOnly Property Exceptions() As System.Collections.ObjectModel.ReadOnlyCollection(Of Interfaces.Exceptions.IExceptionInformation) Implements Interfaces.Exceptions.IExceptionService.Exceptions
            Get
                Return _ReadOnlyExceptions
            End Get
        End Property

        Public Sub PublishException(ByVal ex As System.Exception) Implements Interfaces.Exceptions.IExceptionService.PublishException
            PublishException(ex, False, False)
        End Sub

        Public Sub PublishException(ByVal ex As System.Exception, ByVal handled As Boolean) Implements Interfaces.Exceptions.IExceptionService.PublishException
            PublishException(ex, handled, False)
        End Sub

        Public Sub PublishException(ByVal ex As System.Exception, ByVal handled As Boolean, ByVal displayed As Boolean) Implements Interfaces.Exceptions.IExceptionService.PublishException
            Dim info As ExceptionInformation

            info = New ExceptionInformation(ex, handled, displayed)

            SyncLock _Exceptions
                _Exceptions.Add(info)
            End SyncLock

            Core.Kernel.Logger.Log("ExceptionService", TraceEventType.Error, ex.ToString)

            RaiseEvent ExceptionPublished(Me, New Interfaces.ExceptionEventArgs(info))
        End Sub

        Public Event ExceptionPublished(ByVal sender As Object, ByVal e As Interfaces.ExceptionEventArgs) Implements Interfaces.Exceptions.IExceptionService.ExceptionPublished
    End Class
End Namespace